package com.topn2;

import com.sun.org.apache.xpath.internal.WhitespaceStrippingElementMatcher;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class TMapper extends Mapper<LongWritable, Text, Tkey, IntWritable> {
    private Tkey tkey = new Tkey();
    private IntWritable mval = new IntWritable();

    private Map<String, String> locMap = new HashMap<>();

    @Override
    protected void setup(Mapper<LongWritable, Text, Tkey, IntWritable>.Context context) throws IOException, InterruptedException {
        String path = context.getCacheFiles()[0].getPath();
        Path locationPath = new Path(path);
        FileSystem fs = locationPath.getFileSystem(context.getConfiguration());

        FSDataInputStream in = fs.open(locationPath);

        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String line;
        while ((line = br.readLine()) != null) {
            String[] arr = line.split("\t");
            locMap.put(arr[0], arr[1]);
        }
    }

    public static void main(String[] args) throws Exception {
        String str = "2019-6-1 22:22:22	36";
        String[] strArr = str.split("\t");
        System.out.println(Arrays.toString(strArr));
        // temp
        System.out.println(strArr[strArr.length - 1]);
        // 获得年月日
        SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd");
        Date date = dfs.parse(strArr[0]);

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        System.out.println(calendar.get(Calendar.YEAR));
        System.out.println(calendar.get(Calendar.MONTH) + 1);
        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));

    }

    @Override
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Tkey, IntWritable>.Context context) throws IOException, InterruptedException {
        try {
            String[] strArr = value.toString().split("\t");
            // temp
            int temp = Integer.valueOf(strArr[strArr.length - 1]);
            // 获得年月日
            SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd");
            Date date = dfs.parse(strArr[0]);

            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            tkey.setYear(calendar.get(Calendar.YEAR));
            tkey.setMonth(calendar.get(Calendar.MONTH) + 1);
            tkey.setDay(calendar.get(Calendar.DAY_OF_MONTH));
            tkey.setTemp(temp);

            mval.set(temp);

            context.write(tkey, mval);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
